import uuid
import random
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TasteLink0.settings")
django.setup()
from datetime import datetime, timedelta
from app.models import (
    User, MerchantInfo, Category, Dish, Image,
    Order, OrderItem
)

# # ================== Step 4: 创建图片（从本地文件读取为二进制）==================
# image1_path = "gongbaojiding.jpg"
# image2_path = "shuizhuyu.jpg"
#
# # 读取本地图片为二进制数据
def read_image_binary(file_path):
    with open(file_path, 'rb') as f:
        return f.read()

# # 插入图片记录
# try:
#     image_data_1 = read_image_binary(image1_path)
#     image_data_2 = read_image_binary(image2_path)
# except FileNotFoundError as e:
#     print(f"❌ 文件未找到：{e}")
#     exit(1)
#
# image_gongbao = Image.objects.create(
#     image_data=image_data_1,
#     mime_type='image/jpeg'  # 确保你的图片确实是 JPEG 格式，否则可改为 'image/png'
# )
# image_shuizhu = Image.objects.create(
#     image_data=image_data_2,
#     mime_type='image/jpeg'
# )
#
# print("✅ 图片已从本地读取并创建")
#
# # ================== Step 5: 将图片关联到具体菜品 ==================
from app.models import DishImage
#
# # 获取你要添加图片的两个菜品（例如 dish_name="菜品1" 和 "菜品2"）
# dish1 = Dish.objects.get(dish_name="菜品1")  # 宫保鸡丁
# dish2 = Dish.objects.get(dish_name="菜品2")  # 水煮肉片
#
# # 创建 DishImage 记录
# DishImage.objects.create(dish=dish1, image=image_gongbao, sort_order=0)
# DishImage.objects.create(dish=dish2, image=image_shuizhu, sort_order=0)
#
# print("✅ 图片已成功关联到菜品")


# ================== Step 4补充: 创建更多图片（从本地文件读取为二进制）==================
image3_path = "ningmengshui.jpg"
image4_path = "niuroumian.jpg"
image5_path = "zhashutiao.jpg"
image6_path = "zhuroujiaozi.jpeg"

# 插入更多图片记录
try:
    image_data_3 = read_image_binary(image3_path)
    image_data_4 = read_image_binary(image4_path)
    image_data_5 = read_image_binary(image5_path)
    image_data_6 = read_image_binary(image6_path)
except FileNotFoundError as e:
    print(f"❌ 文件未找到：{e}")
    exit(1)

image_ningmeng = Image.objects.create(
    image_data=image_data_3,
    mime_type='image/jpeg'
)
image_niurou = Image.objects.create(
    image_data=image_data_4,
    mime_type='image/jpeg'
)
image_zhashutiao = Image.objects.create(
    image_data=image_data_5,
    mime_type='image/jpeg'
)
image_zhuroujiaozi = Image.objects.create(
    image_data=image_data_6,
    mime_type='image/jpeg'
)

print("✅ 补充图片已从本地读取并创建")

# ================== Step 5补充: 将新图片关联到具体菜品 ==================
# 获取你要添加图片的菜品3-6
dish3 = Dish.objects.get(dish_name="菜品3")  # 柠檬水
dish4 = Dish.objects.get(dish_name="菜品4")  # 牛肉面
dish5 = Dish.objects.get(dish_name="菜品5")  # 炸薯条
dish6 = Dish.objects.get(dish_name="菜品6")  # 猪肉饺子

# 创建 DishImage 记录
DishImage.objects.create(dish=dish3, image=image_ningmeng, sort_order=0)
DishImage.objects.create(dish=dish4, image=image_niurou, sort_order=0)
DishImage.objects.create(dish=dish5, image=image_zhashutiao, sort_order=0)
DishImage.objects.create(dish=dish6, image=image_zhuroujiaozi, sort_order=0)

print("✅ 补充图片已成功关联到菜品3-6")